<template>
  <van-area :area-list="areaList" @confirm="onConfirm" @cancel="$emit('close')"/>
</template>

<script>
import areaList from '@/utils/areaList'
import { updateUserProfileAPI } from '@/api'

export default {
  name: 'AreaEdit',

  computed: {
    areaList () {
      return areaList
    }
  },
  methods: {
    async onConfirm (res, index) {
      try {
        // console.log(res) // 打印选中的省市区数组对象
        const area = res[0].name + '-' + res[1].name + '-' + res[2].name
        // const newArea = res[2].code
        await updateUserProfileAPI({
          area
        })
        // 更新视图
        this.$emit('update:userArea', area)
        // 关闭弹框
        this.$emit('close')
      } catch (error) {
        console.log(error)
        this.$toast.fail('修改失败')
      }
    }
  }
}
</script>

<style scoped>

</style>
